<?php
 jimport('joomla.application.component.view');
 // Load the view framework
 if(!class_exists('VmView'))require(JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers'.DS.'vmview.php');
 jimport('joomla.html.pane');
class svViewAccount extends JView
{
	function display()
	{
		$task = JRequest::getCMD('task');
		switch ($task)
		{
			case 'add':
				$this->_add();
				break;
			case 'edit':
				$this->_edit();
				break;
			case 'save':
				$this->_save();
				break;
			case 'cancel':
				$this->_cancel();
				break;
			case 'remove':
				$this->_remove();
				break;
			default :
				$this->_default();
				break;
		}
		parent::display();
	}
	// hien thi chi tiet
	function _default()
	{
		// them toolbar
		$this->_gridToolbar();
		// ket noi voi model
		$model=$this->getModel('account');
		$action=JRequest::getVar('action');
		if($action=='')
		{
			// cac bien gia tri
			$result=$model->getAllAccount();
			$title='Quản lý các tài khoản truy cập';
			// set layout
			$this->assignRef('dataObject', $result);
			$this->assignRef('title', $title);
			$this->setLayout('default');
		}
		else
		{
			if($action=='add')
			{
				$this->_formToolbar('Thêm mới một Account');
				// chon cac donviquanly
				$model=$this->getModel('account');
				$donviquanlyList=$model->getDonviquanlyList();
				$iddonvi=JRequest::getInt('iddonvi');
				$phongbanList=$model->getPhongbanList($iddonvi);
				$idphongban=JRequest::getInt('idphongban');
				$canboList=$model->getCanboList($iddonvi,$idphongban);
				// set layout
				$this->assignRef('donviquanlyList', $donviquanlyList);
				$this->assignRef('phongbanList', $phongbanList);
				$this->assignRef('canboList', $canboList);
				$this->assignRef('iddonvi', $iddonvi);
				$this->assignRef('idphongban', $idphongban);
				$this->setLayout('add');
			}
			else
			 {
			 	// toolbar
			 	$this->_formToolbar('Chỉnh sửa Account');
			 	// lay id tu form default
			 	$idaccount=JRequest::getInt('idaccount');
			 	// lay du lieu tu model
			 	$model=$this->getModel('account');
			 	$account=$model->getAccountByID($idaccount);

			 	// set layout
			 	$this->assignRef('account', $account);
			 	$this->setLayout('edit');
			}
		}

	}
	function _add()
	{
		header('location:index.php?option=com_svquanlycanbo&view=account&action=add');
	}
	function _edit()
	{
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idaccount     = $idlist[0];
		header('location:index.php?option=com_svquanlycanbo&view=account&action=edit&idaccount='.$idaccount);
	}
	function _save()
	{
		// lay du lieu
		$idaccount=JRequest::getInt('idaccount');
		$idcanbo=JRequest::getInt('idcanbo');
		$username=JRequest::getVar('username');
		$password=JRequest::getVar('password');
		$email=JRequest::getVar('email');
		$typeaccount=JRequest::getVar('typeaccount');
		// xu ly yeu cau edit/insert
		$model=$this->getModel('account');
		if($idaccount==0)// xac thuc viec them mot item
		{
			$exec=$model->addNewAccount($idcanbo,$username,$password,$email,$typeaccount);
		}
		else//xac thuc viec edit mot item
		{
			$exec=$model->editAccountt($idaccount,$idcanbo,$username,$password,$email,$typeaccount);
		}
		echo $exec;
		header('Location:index.php?option=com_svquanlycanbo&view=account');
	}
	function _cancel()
	{
		header('Location:index.php?option=com_svquanlycanbo&view=account');
	}
	function _remove()
	{
		// lay id tu form default
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idaccount    = $idlist[0];
		// thuc hien yeu cau remove
		$model=$this->getModel('account');
		$exec=$model->delAccount($idaccount);
		echo $exec;
		$this->_default();
		
	}
	// them cac button tren thanh toolbar
	function _gridToolbar()
	{
		JToolBarHelper::title('Quản lý các phòng ban.!');
		JToolBarHelper::addNew();
		JToolBarHelper::editListX();
		JToolBarHelper::deleteList();
	}
	function _formToolbar($title)
	{
		JToolBarHelper::title($title);
		JToolBarHelper::save();
		JToolBarHelper::cancel();
	}
	
}
?>